package com.tally.server.service;

import com.tally.server.domain.ExpDetail;
import com.tally.server.domain.ExpDetailExample;
import com.tally.server.dto.ExpDetailDTO;
import com.tally.server.dto.PageDTO;
import com.tally.server.dto.SysExpDetailDTO;
import com.tally.server.enums.YesNoEnum;
import com.tally.server.mapper.ExpDetailMapper;
import com.tally.server.utils.CopyUtil;
import com.tally.server.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ExpDetailService {

    @Autowired
    private ExpDetailMapper expDetailMapper;

    /**
     * 列表查询
     */
    public void list(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        ExpDetailExample expDetailExample = new ExpDetailExample();
        List<ExpDetail> expDetailList = expDetailMapper.selectByExample(expDetailExample);
        PageInfo<ExpDetail> pageInfo = new PageInfo<>(expDetailList);
        pageDTO.setTotal(pageInfo.getTotal());
        List<ExpDetailDTO> expDetailDTOList = CopyUtil.copyList(expDetailList, ExpDetailDTO.class);
        pageDTO.setList(expDetailDTOList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(ExpDetailDTO expDetailDTO) {
        ExpDetail expDetail = CopyUtil.copy(expDetailDTO, ExpDetail.class);
        if (StringUtils.isEmpty(expDetailDTO.getId())) {
            this.insert(expDetail);
        } else {
            this.update(expDetail);
        }
    }

    /**
     * 新增
     */
    private void insert(ExpDetail expDetail) {
        expDetail.setCreateTime(new Date());
        expDetail.setId(UUIDUtil.getShortUUID());
        expDetailMapper.insert(expDetail);
    }

    /**
     * 更新
     */
    private void update(ExpDetail expDetail) {
        expDetailMapper.updateByPrimaryKey(expDetail);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        expDetailMapper.deleteByPrimaryKey(id);
    }

    public Integer addExp(Integer userId,SysExpDetailDTO sysExpDetailDTO) {
        if(sysExpDetailDTO == null){
            return 0;
        }
        if(sysExpDetailDTO.getOnce().equals(YesNoEnum.YES.getCode())){
            ExpDetail expDetail = getByUserIdAndTypeAndDate(userId, sysExpDetailDTO.getExpType(), null);
            if(expDetail == null){
                // 可以加经验
                expDetail = new ExpDetail();
                expDetail.setUserId(userId);
                expDetail.setExpType(sysExpDetailDTO.getExpType());
                expDetail.setExpValue(sysExpDetailDTO.getExpValue());
                insert(expDetail);
                return sysExpDetailDTO.getExpValue();
            }
        }else{
            ExpDetail expDetail = getByUserIdAndTypeAndDate(userId, sysExpDetailDTO.getExpType(), new Date());
            if(expDetail == null){
                // insert 新增加经验
                expDetail = new ExpDetail();
                expDetail.setUserId(userId);
                expDetail.setExpType(sysExpDetailDTO.getExpType());
                expDetail.setExpValue(sysExpDetailDTO.getExpValue());
                insert(expDetail);
                return sysExpDetailDTO.getExpValue();
            }else {
                // update 通过比对最大经验值与已加经验值决定是否加经验
                Integer expValue;
                if(! (expValue = expDetail.getExpValue()).equals(sysExpDetailDTO.getExpMaxValue())){
                    // 可以加经验
                    expValue += sysExpDetailDTO.getExpValue();
                    expDetail.setExpValue(expValue);
                    update(expDetail);
                    return sysExpDetailDTO.getExpValue();
                }
            }
        }
        return 0;
    }

    private ExpDetail getByUserIdAndTypeAndDate(Integer userId,String type,Date date){
        ExpDetailExample expDetailExample = new ExpDetailExample();
        ExpDetailExample.Criteria criteria = expDetailExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andExpTypeEqualTo(type);
        if(date != null){
            criteria.andCreateTimeEqualTo(date);
        }
        List<ExpDetail> expDetailList = expDetailMapper.selectByExample(expDetailExample);
        if(CollectionUtils.isEmpty(expDetailList)){
            return null;
        }
        return expDetailList.get(0);
    }
}
